from typing import Union, List, Optional
from fastapi import FastAPI, Form, File, UploadFile, Request
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field, ValidationError, validator, validators, EmailStr
import uvicorn
from datetime import date
import sys
import os


app = FastAPI()
app.mount("/static", StaticFiles(directory="static"))


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Union[str, None] = None

class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: Union[str, None] = None

class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: float = 10.5
    tags: List[str] = []

items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "bax": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}

@app.post("/user", response_model=UserOut)
async def create_user(user: UserIn):
    # 存到数据库
    return user

@app.get("/items/01{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]


# response_model_exclude
@app.get("/items02/{item_id}", response_model=Item, response_model_exclude={"description"})
async def read_item(item_id: str):
    return items[item_id]

# response_model_include
@app.get("/items/03{item_id}", response_model=Item, response_model_include={"name", "price"})
async def read_item(item_id: str):
    return items[item_id]


if __name__ == '__main__':
    print(sys.argv)

    # 获取当前文件的完整路径
    file_path = __file__
    # 获取文件名
    file_name = os.path.basename(file_path)
    print(file_name)

    start_args = file_name.split(".py")[0] + ":app"

    uvicorn.run(start_args, host="127.0.0.1", port=8000, reload=True)